home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / mfboid1s / MFBOID1S.ZIP / uBoidEngine.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-17  |  2.5 KB  |  104 lines

  1. unit uBoidEngine;
  2.  
  3. interface
  4.  
  5. uses
  6.     Graphics, Classes, uTMovableEngine, uTMovable, uBoids;
  7.  
  8. type
  9.   TBoidEngine = class(TMovableEngine)
  10.     LineLength            : real;
  11.  
  12.       OptimalDistance : real;
  13.     StayInCenter        : real;
  14.       TooClose                 : real;
  15.       ReallyClose         : real;
  16.       MaxTurnSpeed         : real;
  17.  
  18. //    procedure RunStep;
  19.     procedure CopySettingsToBoid(Boid : TBoid);
  20.     procedure CopySettingsToAllBoids;
  21.  
  22.     constructor Create(iBoidCount : integer; OutputCanvas : TCanvas);
  23.     procedure AdjustMovableCount(iMovableCount : integer);
  24.   end;
  25.  
  26. implementation
  27.  
  28. //******************************************************************************
  29. constructor TBoidEngine.Create(iBoidCount : integer; OutputCanvas : TCanvas);
  30. var
  31.   i                 : integer;
  32.   NewBoid     : TBoid;
  33. begin
  34.     inherited Create(OutputCanvas);
  35.  
  36.   MaxSpeed := 5;
  37.   MaxSpeedChange := 0.09;
  38.   SensorDistance := 60;
  39.  
  40.   OptimalDistance := 20;
  41.   TooClose := 19;
  42.   ReallyClose := 12;
  43.   MaxTurnSpeed := 0.05;
  44.  
  45.   LineLength := 3;
  46.  
  47.   for i := 1 to iBoidCount do
  48.   begin
  49.       NewBoid := TBoid.Create(Canvas);
  50.     MovableList.Add(NewBoid);
  51.  
  52.         CopySettingsToBoid(NewBoid);
  53.   end;
  54. end;
  55.  
  56. //******************************************************************************
  57. procedure TBoidEngine.CopySettingsToAllBoids;
  58. var
  59.     i : integer;
  60. begin
  61.     for i := 0 to MovableList.Count - 1 do
  62.       CopySettingsToBoid(TBoid(MovableList[i]));
  63. end;
  64.  
  65. //******************************************************************************
  66. procedure TBoidEngine.CopySettingsToBoid(Boid : TBoid);
  67. begin
  68.   Boid.MaxSpeed := MaxSpeed;
  69.   Boid.MaxSpeedChange := MaxSpeedChange;
  70.      Boid.SensorDistance := SensorDistance;
  71.  
  72.   Boid.OptimalDistance := OptimalDistance;
  73.   Boid.StayInCenter := StayInCenter;
  74.      Boid.TooClose := TooClose;
  75.   Boid.ReallyClose := ReallyClose;
  76.      Boid.MaxTurnSpeed := MaxTurnSpeed;
  77.   Boid.LineLength := LineLength;
  78. end;
  79.  
  80. //******************************************************************************
  81. procedure TBoidEngine.AdjustMovableCount(iMovableCount : integer);
  82. var
  83.     i                     : integer;
  84.     NewBoid         : TBoid;
  85. begin
  86.   if iMovableCount > MovableList.Count then
  87.   begin
  88.       for i := MovableList.Count to iMovableCount-1 do
  89.       begin
  90.           NewBoid := TBoid.Create(Canvas);
  91.         MovableList.Add(NewBoid);
  92.  
  93.             CopySettingsToBoid(NewBoid);
  94.       end;
  95.   end else
  96.   while iMovableCount < MovableList.Count do
  97.   begin
  98.     TMovable(MovableList[MovableList.Count-1]).Destroy;
  99.     MovableList.Delete(MovableList.Count-1);
  100.     MovableList.Pack;
  101.   end;
  102. end;
  103. end.
  104.